package com.sllp.C71S3TsllpShop.biz.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.ProductExample;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.SmallType;
import com.sllp.C71S3TsllpShop.bean.SmallTypeExample;
import com.sllp.C71S3TsllpShop.bean.SmallTypeExample.Criteria;
import com.sllp.C71S3TsllpShop.biz.SmallTypeBiz;
import com.sllp.C71S3TsllpShop.dao.SmallTypeMapper;

@Service
public class SmallTypeBizImpl implements SmallTypeBiz {

	@Resource
	private SmallTypeMapper stm;
	
	/**
	 * 主界面各个不同类型商品显示
	 * @return
	 */
	public List<SmallType> findProductByType1(){
		SmallTypeExample ste=new SmallTypeExample();
		Criteria c=ste.createCriteria();
		List<Integer> lists=new ArrayList<Integer>();
		lists.add(22);
		lists.add(40);
		c.andParentIn(lists);
		List<SmallType> list=stm.selectByExample(ste);
		return list;
	}
	
	public List<SmallType> findProductByType2(){
		SmallTypeExample ste=new SmallTypeExample();
		Criteria c=ste.createCriteria();
		List<Integer> lists=new ArrayList<Integer>();
		lists.add(23);
		lists.add(47);
		c.andParentIn(lists);
		List<SmallType> list=stm.selectByExample(ste);
		return list;
	}
	
	public List<SmallType> findProductByType3(){
		SmallTypeExample ste=new SmallTypeExample();
		Criteria c=ste.createCriteria();
		c.andParentEqualTo(9);
		List<SmallType> list=stm.selectByExample(ste);
		return list;
	}
	
	public List<SmallType> findProductByType4(){
		SmallTypeExample ste=new SmallTypeExample();
		Criteria c=ste.createCriteria();
		List<Integer> lists=new ArrayList<Integer>();
		lists.add(5);
		lists.add(6);
		c.andParentIn(lists);
		List<SmallType> list=stm.selectByExample(ste);
		return list;
	}
	
	public List<SmallType> findProductByType5(){
		SmallTypeExample ste=new SmallTypeExample();
		Criteria c=ste.createCriteria();
		List<Integer> lists=new ArrayList<Integer>();
		lists.add(30);
		lists.add(31);
		lists.add(32);
		c.andParentIn(lists);
		List<SmallType> list=stm.selectByExample(ste);
		return list;
	}
	
	public List<SmallType> findProductByType6(){
		SmallTypeExample ste=new SmallTypeExample();
		Criteria c=ste.createCriteria();
		List<Integer> lists=new ArrayList<Integer>();
		lists.add(48);
		lists.add(8);
		c.andParentIn(lists);
		List<SmallType> list=stm.selectByExample(ste);
		return list;
	}
	
	public SmallType findSmallTypeByCondition(String name){
		SmallTypeExample ste=new SmallTypeExample();
		ste.createCriteria()
		.andNameLike(name);
		List<SmallType> smalltypes=stm.selectByExample(ste);
		if(smalltypes.size()>0 && smalltypes!=null){
			return smalltypes.get(0);
		}
		return null;
	}
	@Override
	public List<SmallType> findByParent(int parent){
		SmallTypeExample smallTypeExample = new SmallTypeExample();
		smallTypeExample.createCriteria().andParentEqualTo(parent);
		return stm.selectByExample(smallTypeExample);
	}
	
	public int updateSmallType(SmallType smallType) {
		SmallTypeExample smallTypeExample = new SmallTypeExample();
		Criteria criteria = smallTypeExample.createCriteria();
		if(smallType != null) {
			if(smallType.getId() != null){
				criteria.andIdEqualTo(smallType.getId());
			}
			if(smallType.getParent() != null) {
				criteria.andParentEqualTo(smallType.getParent());
			}
		}
		return stm.updateByExampleSelective(smallType, smallTypeExample);
	}
	
	public Result addSmallType(List<String> smallTypeList,int parent) {
		SmallType smallType = null;
		int code = 0;
		for(String bigTypeName : smallTypeList) {
			smallType = new SmallType(null, bigTypeName,parent, 1);
			code = stm.insertSelective(smallType);
			if(code <=0) {
				return new Result(0, "添加失败");
			}
		}
		return new Result(1, "添加成功");
	}
}
